package sqlengine.groupby;

public class ConditionAtom extends Atom {
	private ColumnAtom tableName;

	private ColumnAtom condition ;
	//"c" for table and constant; 
	//"t" for table and table
	private String conditionType = null; 

	/**
	 * Default constructor
	 * 
	 * @param tableStr
	 *            the name of the table referenced by this
	 *            <code>ConditionAtom</code>
	 * @param condStr
	 *            the name of the condition used 
	 *            by this <code>ConditionAtom</code>
	 */
	public ConditionAtom(ColumnAtom tableStr, ColumnAtom condStr, String condType) {		
		tableStr.all_to_lower();
		tableName = tableStr;
		
		conditionType = condType;
		if(condType.equals("t")){ //to lower case
			condStr.all_to_lower();
			condition = condStr;
		}else{
			condStr.tablename_to_lower();
			condition = condStr;
		}				
	}
	
	/**
	 * Gets the name of the table referenced 
	 * by this <code>ConditionAtom</code>	 * 
	 * @return the table name
	 */
	public ColumnAtom getTableName() {
		return tableName;
	}

	/**
	 * Gets the name of the condition used 
	 * by this <code>ConditionAtom</code>
	 * 
	 * @return the name of condition
	 */
	public ColumnAtom getCondition() {
		return condition;
	}
	
	public String getConditionType(){
		return conditionType;
	}
	
	/**
	 * @see edu.buffalo.db.parser.sqlexpr.Atom#getType()
	 */
	@Override
	public int getType() {
		return Atom.CONDITION_ATOM;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((condition == null) ? 0 : condition.hashCode());
		result = PRIME * result
				+ ((tableName == null) ? 0 : tableName.hashCode());
		return result;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		final ConditionAtom other = (ConditionAtom) obj;
		if (condition == null) {
			if (other.condition != null)
				return false;
		} else if (!condition.equals(other.condition))
			return false;
		if (tableName == null) {
			if (other.tableName != null)
				return false;
		} else if (!tableName.equals(other.tableName))
			return false;
		if (conditionType == null) {
			if (other.conditionType != null)
				return false;
		} else if (!conditionType.equals(other.conditionType))
			return false;
		return true;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		if(conditionType == "c")
			return tableName.toString() + " = " +  condition.toString();
		else if(conditionType == "t")
			return tableName.toString() + " = " + condition.toString();
		
		return "To string: No condition Type\n";
	}
}
